<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Binh
 * Date: 1/6/14
 * Time: 2:14 PM
 * To change this template use File | Settings | File Templates.
 */
use \Model;

class Controller_Search extends Controller{
    private $view;

    public function __construct(){
        //Cài đặt view
        $this->view = View::forge('home/layout');
        $this->view->head = View::forge('home/head');
        $this->view->header = View::forge('home/header');
        $this->view->footer = View::forge('home/footer');
        $this->view->layout_search = View::forge('home/layout_search');

        //Cài đặt biến toàn cục
        $this->view->set_global('site', 'layout_search');
        $this->view->set_global('error', 'Không tìm thấy kết quả');
    }

    public function action_index(){
        return $this->set_layout($_GET['searchData']);
    }

    private function set_layout($chars){
        $singers = $this->array_slice(\Model\Singer::search_name($chars), 3);
        $songs = $this->array_slice(\Model\Song::search_name($chars),7);
        $this->view->set_global('singers', $singers);
        $this->view->set_global('songs', $songs);

        return $this->view;
    }

    //Lấy mảng con từ màng cha, bắt đầu từ 0 tới end
    //Giá trị nhập vào $data, $end
    //Kết quả array(array())
    private function array_slice($data, $end){
        $list = array();
        for($i = 0; $i < count($data); $i++){
            if($i > $end){
                break;
            }
            $list[] = $data[$i];
        }
        return $list;
    }
}